package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.Location;
import org.flywaydb.core.api.configuration.ClassicConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class FlywayDbInitializer {

    @Autowired
    DataSource dataSource;

    @PostConstruct
    public void migrateFlyway(){

        ClassicConfiguration  configuration = new ClassicConfiguration();
        configuration.setDataSource(dataSource);
        configuration.setTable("service1_flyway_schema_history");
        configuration.setBaselineOnMigrate(true);
        configuration.setCleanOnValidationError(false);
        configuration.setIgnoreIgnoredMigrations(true);
        configuration.setIgnoreMissingMigrations(true);
        configuration.setEncoding(StandardCharsets.UTF_8);
        configuration.setLocations(new Location("db/migration123"));
        Flyway alertFlyway = new Flyway(configuration);
        alertFlyway.migrate();

    }


}
